from mini_tools.ftpLoad import upgt
import os,re

version='5.4.7'

def replaceText(file_path,pat:re.Pattern,replacement_text):


    try:
        # 打开文件以读取其内容
        with open(file_path, 'r',encoding='utf-8') as file:
            file_contents = file.read()

        # 执行文本替换
        updated_contents = pat.sub(replacement_text,file_contents)

        # 关闭原始文件
        file.close()

        # 打开文件以写入替换后的内容
        with open(file_path, 'w',encoding='utf-8') as file:
            file.write(updated_contents)

        # 关闭文件
        file.close()

        print(f"文件 '{file_path}' 中的文本已被替换。")
    except Exception as e:
        print(f"发生错误: {str(e)}")

def dbwin10():
    #替换更新文件的版本 updateExe.py
    pat0=re.compile(r"(?<=current_version = ').*?(?=')")
    pat1=re.compile(r"(?<=win_version = ').*?(?=')")
    replaceText('updateExe.py',pat0,version)
    replaceText('updateExe.py',pat1,'win10')

    #替换gt.spec
    pat2=re.compile(r"(?<=name=').*?(?=')")
    replaceText('gt.spec',pat2,f'gt-win10-{version.replace(".","_")}')

    #打包
    os.system(r'C:\Users\Administrator\Desktop\2024\xiapi\xpvenv\Scripts\activate && pyinstaller gt.spec')

    #上传至服务器
    newname=f"gt-win10-{version.replace('.','_')}.exe"
    upgt(newname)

def dbjianron():
    #替换更新文件的版本 updateExe.py
    pat0=re.compile(r"(?<=current_version = ').*?(?=')")
    pat1=re.compile(r"(?<=win_version = ').*?(?=')")
    replaceText('updateExe.py',pat0,version)
    replaceText('updateExe.py',pat1,'ALL')

    #替换gt.spec
    pat2=re.compile(r"(?<=name=').*?(?=')")
    replaceText('gt38.spec',pat2,f'gt-ALL-{version.replace(".","_")}')

    #打包
    os.system(r'C:\Users\Administrator\Desktop\2024\xiapi\py38venv\Scripts\activate && pyinstaller gt38.spec')

    #上传至服务器
    newname=f"gt-ALL-{version.replace('.','_')}.exe"
    upgt(newname)

def thversion():
    with open('dist/version.txt','w',encoding='utf-8') as f:
        f.write(version)
    upgt('version.txt')

def main():
    thversion()
    dbwin10()
    dbjianron()
                
if __name__=='__main__':
    main()